package com.cgsd.heyu.http;

import com.android.library.utils.LogUtils;
import com.cgsd.heyu.App;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.http.interceptor.HeaderInterceptor;
import com.google.gson.Gson;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.File;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * @author xiaoxing
 * Created by Administrator on 2018/3/24.
 */

public class RetrofitServiceManager {
    /**
     * 超时时间
     */
    private static final int DEFAULT_TIME_OUT = 10;
    /**
     * 读取时间
     */
    private static final int DEFAULT_READ_TIME_OUT = 10;
    /**
     * 读取时间
     */
    private static final int DEFAULT_WRITE_TIME_OUT = 10;
    private Retrofit mRetrofit;


    private RetrofitServiceManager(){
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS);
        builder.readTimeout(DEFAULT_READ_TIME_OUT, TimeUnit.SECONDS);
        builder.writeTimeout(DEFAULT_WRITE_TIME_OUT, TimeUnit.SECONDS);
        File cacheDir = new File(App.getInstance().getCacheDir(), "response");
        //缓存的最大尺寸10m
        Cache cache = new Cache(cacheDir, 1024 * 1024 * 1024);
        builder.cache(cache);
        //设置统一的header
        builder.interceptors().add(new HeaderInterceptor());
        //禁止代理
        builder.proxy(Proxy.NO_PROXY);
        addInterceptor(builder);


        mRetrofit = new Retrofit.Builder()
                .client(builder.build())
                .baseUrl(AppConfig.BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(ResponseConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    /**
     * 添加各种拦截器
     * @param builder
     */
    private void addInterceptor(OkHttpClient.Builder builder) {
        // 添加日志拦截器，非debug模式不打印任何日志
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印日志
                LogUtils.d("RetrofitLog","retrofitbace = "+message);
//                com.apkfuns.logutils.LogUtils.e(""+"retrofitbace = "+message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);

    }

    private static class SingletonHolder{
        private static RetrofitServiceManager retrofitServiceManager = new RetrofitServiceManager();
    }

    public static RetrofitServiceManager getInstance(){
        return SingletonHolder.retrofitServiceManager;
    }

    public <T> T creat(Class<T> tClass){
        return mRetrofit.create(tClass);
    }

    /**
     * 构建body参数
     * @param data
     * @return
     */
    public static RequestBody getBody(Object data){
        String json = new Gson().toJson(data);
        return RequestBody.create(json,okhttp3.MediaType.parse("application/json; charset=utf-8"));
    }

}
